/**
 * 添加或者修改页面
 */
var TbFreightInfoDlg = {
    data: {
        area: "",
        destination: "",
        firstWeight: "",
        continuesWeight: "",
        arrivalTime: ""
    }
};


layui.use(['form', 'admin', 'ax', 'formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var formSelects = layui.formSelects;


    $.ajax({
        url: Feng.ctxPath + "/region/listsByS",
        success: function (data) {
            var html = '<option value="" >' + "请选择" + '</option>';
            // 加载数据
            for (var i = 0; i < data.data.length; i++) {
                html += '<option value="' + data.data[i].regionCode + '">'
                    + data.data[i].regionName + '</option>'
            }
            $("#area").append(html);
            form.render();
        }
    });

    //省
    form.on('select(area)', function (data) {
        var keys = [];
        $.ajax({
            url: Feng.ctxPath + "/region/listsByP?parentCode=" + data.value,
            success: function (data) {
                // 加载数据
                /*for (var i = 0; i < data.data.length; i++) {
                    var temp = {"name":data.data[i].regionName,"value":data.data[i].regionName}
                    keys.push(temp);
                }
            formSelects.data('select1', 'local', {
                arr: keys
            });*/
                var html = '<option value="" >' + "请选择" + '</option>';
                // 加载数据
                for (var i = 0; i < data.data.length; i++) {
                    html += '<option value="' + data.data[i].regionCode + '">'
                        + data.data[i].regionName + '</option>'
                }
                $("#destination").append(html);
                form.render();
            }
        });
    });

    //市
    form.on('select(destination)', function (data) {
        var keys = [];
        $.ajax({
            url: Feng.ctxPath + "/region/listsByP?parentCode=" + data.value,
            success: function (data) {
                /*var html = '<option value="" >' + "请选择" + '</option>';
                // 加载数据
                for (var i = 0; i < data.data.length; i++) {
                    html += '<option value="' + data.data[i].regionCode + '">'
                        + data.data[i].regionName + '</option>'
                }
                $("#city").append(html);
                form.render();*/
                // 加载数据
                for (var i = 0; i < data.data.length; i++) {
                    var temp = {"name":data.data[i].regionName,"value":data.data[i].regionName}
                    keys.push(temp);
                }
            formSelects.data('select1', 'local', {
                arr: keys
            });
            }
        });
    });

    $.ajax({
        url: Feng.ctxPath + "/weightPrice/lists",
        success: function (data) {
            var html = '<option value="" >' + "请选择" + '</option>';
            // 加载数据
            for (var i = 0; i < data.data.length; i++) {
                html += '<option value="' + data.data[i].id + '">'
                    + data.data[i].name + '</option>'
            }
            $("#weightPriceId").append(html);
            form.render();
        }
    });


    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/tbFreight/addItem", function (data) {
            Feng.success("添加成功！");
            window.location.href = Feng.ctxPath + '/tbFreight'
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });


    //返回按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/tbFreight'
    });

});

